package com.ray.quartz.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.quartz.common.Constant;
import com.ray.quartz.service.ScheduleJobService;
import com.ray.quartz.table.entity.ScheduleJob;
import com.ray.quartz.table.mapper.ScheduleJobMapper;
import com.ray.quartz.utils.ScheduleUtils;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;

@Service
public class ScheduleJobServiceImpl implements ScheduleJobService {
	@Autowired
    private Scheduler scheduler;
	@Autowired
	private ScheduleJobMapper scheduleJobMapper;
	
	/**
	 * 项目启动时，初始化定时器
	 */
	@PostConstruct
	public void init(){
		List<ScheduleJob> scheduleJobList = scheduleJobMapper.queryList(new ScheduleJob());
		for(ScheduleJob scheduleJob : scheduleJobList){
			CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
            //如果不存在，则创建
            if(cronTrigger == null) {
                ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
            }else {
                ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
            }
		}
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public ScheduleJob queryObject(Long jobId) {
		return scheduleJobMapper.queryObject(jobId);
	}

	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<ScheduleJob> queryList(PageDto<ScheduleJob> pageDto, ScheduleJob params,LoginUser user) {
		if(!user.isSuperManager()){
			params.setTenantId(user.getTenantId());
		}
		return scheduleJobMapper.queryPageList(pageDto,params);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public int queryTotal(ScheduleJob params) {
		return scheduleJobMapper.queryTotal(params);
	}
	

	
	@Transactional
	public void save(ScheduleJob scheduleJob,LoginUser currentUser) {
		scheduleJob.setCreateTime(new Date());
		scheduleJob.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
		scheduleJob.setBapid(currentUser.getOrgCode());
		scheduleJob.setBaid(currentUser.getDeptCode());
		scheduleJob.setCreateId(currentUser.getUserId());
		scheduleJob.setCreateTime(new Date());
		scheduleJob.setTenantId(currentUser.getTenantId());
		scheduleJobMapper.save(scheduleJob);
        
        ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
    }
	
	
	@Transactional
	public void update(ScheduleJob scheduleJob,LoginUser currentUser) {
        ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
        scheduleJob.setUpdateId(currentUser.getUserId());
        scheduleJob.setUpdateTime(new Date());
        scheduleJobMapper.update(scheduleJob);
    }

	
	@Transactional
    public void deleteBatch(Long[] jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.deleteScheduleJob(scheduler, jobId);
    	}
    	//删除数据
    	scheduleJobMapper.deleteBatch(jobIds);
	}

	@Transactional
    public int updateBatch(Long[] jobIds, int status){
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("list", jobIds);
    	map.put("status", status);
    	return scheduleJobMapper.updateBatch(map);
    }
    
	
	@Transactional
    public void run(Long[] jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.run(scheduler, queryObject(jobId));
    	}
    }

	
	@Transactional
    public void pause(Long[] jobIds) {
        for(Long jobId : jobIds){
    		ScheduleUtils.pauseJob(scheduler, jobId);
    	}
    	updateBatch(jobIds, Constant.ScheduleStatus.PAUSE.getValue());
    }

	
	@Transactional
    public void resume(Long[] jobIds) {
    	for(Long jobId : jobIds){
    		ScheduleUtils.resumeJob(scheduler, jobId);
    	}
    	updateBatch(jobIds, Constant.ScheduleStatus.NORMAL.getValue());
    }

	@Transactional
	public void delete(Long id) {
		Long[] ids = {id};
		this.deleteBatch(ids);
	}

	@Transactional
	public void run(Long id) {
		Long[] ids = {id};
		this.run(ids);
	}

	@Transactional
	public void pause(Long id) {
		Long[] ids = {id};
		this.pause(ids);
	}

	@Transactional
	public void resume(Long id) {
		Long[] ids = {id};
		this.resume(ids);
	}


	
    
}
